﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class NguonNuocService : IService<NguonNuoc>
    {
        private readonly IEntityRepository<NguonNuoc> _nguonNuocRepository;
        public NguonNuocService(IEntityRepository<NguonNuoc> nguonNuocRepository)
        {
            _nguonNuocRepository = nguonNuocRepository;
        }

        public NguonNuoc Create(NguonNuoc t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _nguonNuocRepository.InsertOnCommit(t);
                _nguonNuocRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return t;
        }

        public NguonNuoc Update(NguonNuoc t)
        {
            NguonNuoc item;
            try
            {
                item = _nguonNuocRepository.Get(t.Id);
                item.Ten = t.Ten;
                item.TrongCoSo = t.TrongCoSo;
                item.NgoaiCoSo = t.NgoaiCoSo;
                item.KhoiTich = t.KhoiTich;
                item.XeCC = t.XeCC;
                item.MBCC = t.MBCC;
                item.PhanLoaiChiTietNguonNuocId = t.PhanLoaiChiTietNguonNuocId;
                item.PhieuDieuTraCoSoesId = t.PhieuDieuTraCoSoesId;
                //item.Created = DateTime.Now;
                //item.CreatedBy = t.CreatedBy;
                _nguonNuocRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public NguonNuoc Detail(Guid id)
        {
            var item = _nguonNuocRepository.Get(id);
            return item;
        }

        public List<NguonNuoc> ToList()
        {
            var item = _nguonNuocRepository.GetAll().ToList();
            return item;
        }

        public void Delete(NguonNuoc t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _nguonNuocRepository.DeleteOnCommit(t);
                _nguonNuocRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
